﻿//Sorting an array means to arrange its elements in increasing order. Write a program to sort an array. Use the "selection sort" algorithm: Find the smallest element, move it at the first position, find the smallest from the rest, move it at the second position, etc.


using System;

namespace SelectionSortArray
{
    class SelectionSortArray
    {
        static void Main()
        {
            int arraySize = 10;
            int[] array = new int[10] { 100, 50, 20, 40, 10, 60, 80, 70, 90, 30 };


            Console.WriteLine("The array before Selection Sort is: ");
            for (int i = 0; i < arraySize; i++)
            {
                Console.WriteLine("array[{0}] =  {1}",i, array[i]);
            }

            // use of the selection sort
            int tmp, minKey;

            for (int j = 0; j < arraySize - 1; j++) //int[] array = new int[10] { 100, 50, 20, 40, 10, 60, 80, 70, 90, 30 };
            {
                minKey = j;

                for (int k = j + 1; k < arraySize; k++)
                {
                    if (array[k] < array[minKey])
                    {
                        minKey = k;
                    }
                }

                tmp = array[minKey];
                array[minKey] = array[j];
                array[j] = tmp;
            }

            Console.WriteLine("The array after Selection Sort is: ");
            for (int i = 0; i < 10; i++)
            {
                Console.WriteLine("array[{0}] =  {1}", i, array[i]);
            }
        }
    }
}
